package com.wl.week_01.Homework_01;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * 图书馆类
 * @author TaiZai
 * @data 2024/9/20 17:49
 */
public class Library {
    private List<Book> books;

    public void addBook(Book book) {
        // 加入一本书籍
        books.add(book);
    }

    public void showBooks() {
        // 展示所有书籍
        System.out.println("--------------------------------------------------------------");
        String header = String.format("%-5s\t%-5s\t %-5s\t\t%-5s\t\t%-5s\t%-5s\t%-5s",
                "书名", "类型", "作者", "价格", "页数", "大小", "状态");
        System.out.println(header);
        System.out.println("--------------------------------------------------------------");

        for (Book book : books) {
            System.out.println(book.getDetails());
        }
        System.out.println("--------------------------------------------------------------");
    }

    public void rentBook(String title) {
        // 根据书名租借图书
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                // 检查是否为电子书
                if (book instanceof EBook) {
                    System.out.println("《" + title + "》是电子书，无需借阅");
                } else if (book instanceof PrintedBook printedBook) {
                    printedBook.rent();
                }
                return;
            }
        }
        throw new NoSuchElementException("未找到该图书");
    }

    public Library() {
        // 初始化书籍列表
        this.books = new ArrayList<>();
    }
}
